package day4.p1;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class FileCopy {

	public static void main(String[] args) throws IOException {
		if (args.length != 2) {
			System.out.println("Insufficient arguments");
			System.out.println("Usage: Filecopy <source> <destination>");
			System.exit(0);
		}
		/*
		 * File ff1 = new File(args[0]); if (!(ff1.exists())) {
		 * System.out.println("File Not Found: " + args[0]); System.exit(0); }
		 * else if (!(ff1.canRead())) { System.out.println("Cannot read file: "
		 * + args[0]); System.exit(0); } File ff2 = new File(args[1]); if
		 * (ff2.exists()) { System.out .print("File \"" + args[1] +
		 * "\" already exists. whether you want to overwrite?(Yes/No)"); Scanner
		 * s = new Scanner(System.in); String ch = s.nextLine(); if
		 * (ch.charAt(0) == 'n' || ch.charAt(0) == 'N') System.exit(0); }
		 * BufferedInputStream bis = new BufferedInputStream(new
		 * FileInputStream(args[0])); BufferedOutputStream bos = new
		 * BufferedOutputStream(new FileOutputStream(args[1]));
		 * 
		 * // Copy operation is done here int ch; while ((ch = bis.read()) !=
		 * -1) bos.write(ch); bos.flush(); bis.close(); bos.close();
		 * System.out.println("File Copied successfully.");
		 */

		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(args[0]));
			bos = new BufferedOutputStream(new FileOutputStream(args[1]));
			File ff2 = new File(args[1]);
			if (ff2.exists()) {
				System.out
						.print("File \""
								+ args[1]
								+ "\" already exists. whether you want to overwrite?(Yes/No)");
				Scanner s = new Scanner(System.in);
				String ch = s.nextLine();
				if (ch.charAt(0) == 'n' || ch.charAt(0) == 'N') System.exit(0);
			}
			int ch;
			while ((ch = bis.read()) != -1)
				bos.write(ch);
			bos.flush();
			bis.close();
			bos.close();
			System.out.println("File Copied successfully.");
		} catch (FileNotFoundException e) {
			System.out.println("Error 404: File Not Found.");
		}
	}
}
